package interfaces;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.SwingConstants;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.CardLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JDialog;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UIManager;

import proyecto.GestorTienda;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class InicioSesion extends JDialog {

	private JPanel contentPane;
	private JPanel panel;
	private JTextField textUser;
	private JPasswordField textPass;
	private JPanel panelAceptarCancelar;
	private JButton btnAceptar;
	private JButton btnSalir;
	private JPanel panelNorte;
	private JLabel infoVentana;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					InicioSesion frame = new InicioSesion();
					frame.setVisible(true);
					frame.setResizable(false);
				    frame.setLocationRelativeTo(null);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public InicioSesion() {
		setTitle("Inicio Sesión");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 430, 168);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new GridLayout(1, 0, 0, 0));
		
		panel = new JPanel();
		contentPane.add(panel);
		panel.setLayout(new BorderLayout(0, 0));
		
		JPanel panelRellenar = new JPanel();
		panel.add(panelRellenar, BorderLayout.CENTER);
		
		JLabel pedirUser = new JLabel("Usuario:");
		pedirUser.setForeground(new Color(128, 0, 128));
		
		textUser = new JTextField();
		textUser.setColumns(10);
		
		JLabel pedirPass = new JLabel("Contraseña:");
		pedirPass.setForeground(new Color(128, 0, 128));
		
		textPass = new JPasswordField();
		GroupLayout gl_panelRellenar = new GroupLayout(panelRellenar);
		gl_panelRellenar.setHorizontalGroup(
			gl_panelRellenar.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelRellenar.createSequentialGroup()
					.addGap(90)
					.addGroup(gl_panelRellenar.createParallelGroup(Alignment.TRAILING)
						.addComponent(pedirPass)
						.addComponent(pedirUser))
					.addGap(35)
					.addGroup(gl_panelRellenar.createParallelGroup(Alignment.LEADING)
						.addComponent(textPass, GroupLayout.PREFERRED_SIZE, 114, GroupLayout.PREFERRED_SIZE)
						.addComponent(textUser, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap())
		);
		gl_panelRellenar.setVerticalGroup(
			gl_panelRellenar.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelRellenar.createSequentialGroup()
					.addGap(7)
					.addGroup(gl_panelRellenar.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panelRellenar.createSequentialGroup()
							.addGap(2)
							.addComponent(pedirUser))
						.addComponent(textUser, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panelRellenar.createParallelGroup(Alignment.BASELINE)
						.addComponent(pedirPass)
						.addComponent(textPass, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(29))
		);
		panelRellenar.setLayout(gl_panelRellenar);
		
		panelAceptarCancelar = new JPanel();
		panel.add(panelAceptarCancelar, BorderLayout.SOUTH);
		panelAceptarCancelar.setLayout(new GridLayout(1, 0, 0, 0));
		
		btnAceptar = new JButton("Aceptar");
		btnAceptar.addMouseListener(new MouseAdapter() {

			public void mouseClicked(MouseEvent e) {
				String pass = new String(textPass.getPassword());
				if (GestorTienda.getGestorTienda().comprobarDatos(textUser.getText(), pass)){
					setVisible(false);
					//
				}
				else{
					textUser.setText("");
					textPass.setText("");
					textUser.grabFocus();
				}
				
			}
		});
		panelAceptarCancelar.add(btnAceptar);
		
		btnSalir = new JButton("Cancelar");
		btnSalir.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				setVisible(false);
			}
		});
		panelAceptarCancelar.add(btnSalir);
		
		panelNorte = new JPanel();
		panel.add(panelNorte, BorderLayout.NORTH);
		panelNorte.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		infoVentana = new JLabel("Introduzca un usuario y su contraseña: ");
		panelNorte.add(infoVentana);
	}
}
